package com.xiguanlezz.cn.common.utils;

import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.util.Set;

/**
 * @Author：习惯了种种
 * @CreateDate：2024/11/28 10:00
 * @Description：
 */
public class ConvertUtils {
    public static final Set<String> TRUE_SET = Sets.newHashSet("y", "yes", "on", "true", "t");
    public static final Set<String> FALSE_SET = Sets.newHashSet("n", "no", "off", "false", "f");


    public static boolean toBoolean(String val, boolean defaultValue) {
        if (StringUtils.isBlank(val)) {
            return defaultValue;
        }
        return Boolean.parseBoolean(val);
    }

    public static boolean toBoolean(final String str) {
        return Boolean.TRUE.equals(toBooleanObject(str));
    }

    public static Boolean toBooleanObject(String str) {
        String formatStr = (str == null ? StringUtils.EMPTY : str).toLowerCase();

        if (TRUE_SET.contains(formatStr)) {
            return true;
        } else if (FALSE_SET.contains(formatStr)) {
            return false;
        } else {
            return null;
        }
    }
}
